home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
clean
/
sun3.lha
/
Sun3
/
seqdemos
/
str_arit.icl
< prev
next >
Wrap
Text File
|
1992-08-07
|
1KB
|
48 lines
MODULE str_arit;
<<
String Arithmetic.
This program demonstrates string arithmetic by mergesorting the characters
of a large string.
>>
FROM deltaS IMPORT <S,+S,LENGTH,SLICE,INDEX;
FROM deltaI IMPORT <=,/,--;
RULE
== *S is needed to create the large string.
:: *S INT STRING -> STRING;
*S 0 string -> "";
*S n string -> +S string (*S (-- n) string);
== The mergesort algorithm on strings.
:: MergeSort STRING -> STRING;
MergeSort str -> str, IF <= length 1
-> Merge (MergeSort first) (MergeSort second),
first: SLICE str 0 (-- middle),
second: SLICE str middle (-- length),
middle: / length 2,
length: LENGTH str;
:: Merge STRING STRING -> STRING;
Merge str "" -> str;
Merge "" str -> str;
Merge str1 str2
-> +S ch1 (Merge (RemoveFirstChar str1) str2), IF <S ch1 ch2
-> +S ch2 (Merge str1 (RemoveFirstChar str2)),
ch1: SLICE str1 0 0,
ch2: SLICE str2 0 0;
:: RemoveFirstChar STRING -> STRING;
RemoveFirstChar string -> SLICE string 1 (-- (LENGTH string));
== The Start rule: sort a large string (30*40 characters).
:: Start -> STRING;
Start -> MergeSort (*S 30 "Sort this garbage properly, please :-). ");